package com.shareyi.baseproject.kernel.domain.common.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Date;

/**
 * 日志值对象，用于记录数据日志信息
 *
 * @author david
 * @date 2020-08-24
 */
@Getter
@Setter
@ToString
public class RecordLogInfo implements Serializable {

    /**
     * 创建人
     */
    private String creator;
    /**
     * 操作人
     */
    private String operator;
    /**
     * 并发版本号，不一定以第三方传入的为准
     */
    private Integer concurrentVersion;
    /**
     * 创建时间，不一定以第三方传入的为准
     */
    private Date created;
    /**
     * 修改时间, 不一定以第三方传入的为准
     */
    private Date modified;

    /**
     * 创建时构建
     *
     * @param creator
     * @return
     */
    public static RecordLogInfo buildWhenCreating(String creator) {
        return buildWhenCreating(creator, new Date());
    }

    /**
     * 创建时构建，传入创建时间
     *
     * @param creator
     * @param createTime
     * @return
     */
    public static RecordLogInfo buildWhenCreating(String creator, Date createTime) {
        RecordLogInfo recordLog = new RecordLogInfo();
        recordLog.creator = creator;
        recordLog.created = createTime;
        recordLog.concurrentVersion = 1;
        return recordLog;
    }

    /**
     * 更新时构建
     *
     * @param operator
     * @return
     */
    public static RecordLogInfo buildWhenUpdating(String operator) {
        return buildWhenUpdating(operator, new Date());
    }

    /**
     * 更新时构建
     *
     * @param operator
     * @return
     */
    public static RecordLogInfo buildWhenUpdating(String operator, Date modifyTime) {
        RecordLogInfo recordLog = new RecordLogInfo();
        recordLog.operator = operator;
        recordLog.modified = modifyTime;
        return recordLog;
    }

    /**
     * 更新
     *
     * @param operator
     */
    public void update(String operator) {
        setOperator(operator);
        setModified(new Date());
        concurrentVersion++;
    }

}
